<template>
	<view>
		<view class="search">
			<my-search></my-search>
		</view>
		<view class="scroll_container">
			<scroll-view class="scroll_title" scroll-y :style="{height: vh + 'px'}">
				<view v-for="(item, i) in cateList" :key="i" :class="['scroll_title_item', i === active ? 'active': '']"
				@click="changeActive(i)">
					{{item.cat_name}}
				</view>
			</scroll-view>
			<scroll-view class="scroll_content" scroll-y :scroll-top="scrollTop" :style="{height: vh + 'px'}">
				<view v-for="(item2, i2) in cate2List" :key="i2">
					<view class="cate2_title">{{item2.cat_name}}</view>
					<view class="cate2_content">
						<view class="cate2_item" v-for="(item3, i3) in item2.children" :key="i3" @click="toGoodsList(item3)">
							<image :src="item3.cat_icon" mode="widthFix"></image>
							<text>{{item3.cat_name}}</text>
						</view>
					</view>
				</view>
			</scroll-view>
		</view>
	</view>
</template>

<script>
	import badgeMix from '@/mixins/tabbarBadge.js'
	export default {
		mixins: [badgeMix],
		data() {
			return {
				cateList: [],
				active: 0,
				cate2List: [],
				scrollTop: 0,
				vh: 0
			};
		},
		onLoad() {
			this.getCateList()
			const sysInfo = uni.getSystemInfoSync()
			this.vh = sysInfo.windowHeight - 50
		},
		methods: {
			async getCateList() {
				const {data} = await uni.$http.get('/api/public/v1/categories')
				if (data.meta.status !== 200) {
					return uni.showMsg('加载失败')
				}
				this.cateList = data.message
				this.cate2List = this.cateList[0].children
				console.log(this.cateList)
			},
			changeActive(index) {
				this.active = index
				this.cate2List = this.cateList[index].children
				this.scrollTop = this.scrollTop === 0 ? 1 : 0
			},
			toGoodsList(item) {
				uni.navigateTo({
					url: '/subpkg/goods_list/goods_list?cid='+ item.cat_id
				})
			}
		}
	}
</script>

<style lang="scss">
// scroll-view {
// 	height: 100vh;
// }
.search {
	height: 50px;
	position: sticky;
	top:0;
	left:0;
}
.scroll_container {
	padding: 0;
	display: flex;
	.scroll_title {
		width: 120px;
		.scroll_title_item {
			height: 60px;
			line-height: 60px;
			text-align: center;
			background-color: #f7f7f7;
			&.active {
				background-color: #fff;
				position: relative;
				&::before {
					content: '';
					width: 3px;
					height: 30px;
					background-color: #d81f07;
					position: absolute;
					left: 0;
					top: 15px;
				}
			}
		}
	}
	.scroll_content {
		padding-top: 20rpx;
		.cate2_title {
			height: 70rpx;
			line-height: 70rpx;
			text-align: center;
			font-weight: 700;
		}
		.cate2_content {
			display: flex;
			flex-wrap: wrap;
			.cate2_item {
				padding: 10px 0;
				width: 33.33%;
				display: flex;
				flex-direction: column;
				justify-content: center;
				align-items: center;
				image {
					width: 70%;
				}
			}
		}
		
	}
}
</style>
